//package com.woniu.gateway.filter;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.hc.utils.JwtUtil;
//import com.hc.utils.ResponseEntity;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//import java.util.concurrent.TimeUnit;
//
///**
// * @ClassName LoginFilter
// * @Description 网关实现统一身份认证
// * @Author shanshan
// * @Date 2022/10/25 9:51
// * @Version 1.0
// **/
//@Component
//public class LoginFilter implements GlobalFilter, Ordered {
//
//    private JwtUtil jwtUtil = new JwtUtil();
//
////    @Autowired
////    RedisTemplate<String,Object> redisTemplate;
//
//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        //进入LoginFilter
////        System.out.println("进入LoginFilter");
//        ServerHttpRequest request = exchange.getRequest();
//        String path = request.getURI().getPath();
//        //Login请求,登录请求放行
//        if(path.contains("/login")){
////          System.out.println("Login请求，直接放行");
//            //添加特定头信息，通过下一级的权限过滤
//            request.mutate().header("login","login").build();
//            return chain.filter(exchange);
//        }
//        //其他请求，验证token
//        String token = request.getHeaders().getFirst("token");
//        if(token != null && jwtUtil.verifyJWT(token)) {
//            //有效token，进行判断redis中是否过期
//            String tokenKey = "token" + jwtUtil.getInfo(token, "id");
//            String userKey = "user" +jwtUtil.getInfo(token, "id");
//            //token过期，返回令牌过期
//            if(stringRedisTemplate.opsForValue().get(tokenKey) == null) {
//                return exchange.getResponse().writeWith(Mono.just(result(exchange,"702","登录过期，请重新登录")));
//            }
//
//            //token未过期，进行token续期，同步续期用户信息缓存
//            stringRedisTemplate.expire(tokenKey,30, TimeUnit.MINUTES);
//            stringRedisTemplate.expire(userKey,30, TimeUnit.MINUTES);
//            return chain.filter(exchange);
//        }
//
//        //无效则拦截
//        return exchange.getResponse().writeWith(Mono.just(result(exchange, "701","先登录")));
//    }
//
//    //拦截，要求先登录
//    public DataBuffer result(ServerWebExchange exchange, String code, String msg) {
//        ServerHttpResponse response = exchange.getResponse();
//        ResponseEntity<String> responseEntity = new ResponseEntity<>(code, msg , null);
//        byte[] bytes = null;
//        try {
//            bytes = new ObjectMapper()
//                    .writeValueAsString(responseEntity).getBytes(StandardCharsets.UTF_8);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
//        DataBuffer dataBuffer = response.bufferFactory().wrap(bytes);
//        response.getHeaders().add("Content-Type", "application/json;charset=utf-8");
//        return dataBuffer;
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//}